package com.hsd.whb.service.impl;

import com.hsd.whb.entity.MeetingMaterals;
import com.hsd.whb.repository.MeetingMateralsRepository;
import com.hsd.whb.service.MeetingMateralsService;
import com.hsd.whb.utils.BeanUtil;
import com.hsd.whb.utils.FileUtil;
import com.wangjiacai.hqlQuery.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MeetingMateralServiceImpl extends BaseServiceImpl implements MeetingMateralsService {

    @Autowired
    private MeetingMateralsRepository repository;

    @Override
    public MeetingMaterals findByMeetingId(Integer meetingId) {
        return repository.findByMeetingId(meetingId);
    }

    @Override
    public MeetingMaterals save(MeetingMaterals meetingMaterals) {
        return repository.save(meetingMaterals);
    }

    @Override
    public List<Map> getMapByMeetingMaterals(MeetingMaterals meetingMaterals) {
        List<Map> list = new ArrayList<>();
        if (meetingMaterals == null) return list;
        setMapToList(list, meetingMaterals, "url");
        setMapToList(list, meetingMaterals, "var1");
        setMapToList(list, meetingMaterals, "var2");
        setMapToList(list, meetingMaterals, "var3");
        setMapToList(list, meetingMaterals, "var4");
        return list;
    }

    private void setMapToList(List<Map> list, Object sources, String param) {
        String path = (String) BeanUtil.getValueByPropertyNames(sources, param);
        if (path != null) {
            Map<String, String> map = new HashMap<>();
            map.put("url", path);
            map.put("fileName", FileUtil.getFileName(path));
            list.add(map);
        }
    }
}
